package com.codi.bus.core.job.impl;

import com.alibaba.fastjson.JSON;
import com.codi.base.cache.CacheUtil;
import com.codi.base.exception.BaseAppException;
import com.codi.bus.constant.CacheKeyConst;
import com.codi.bus.constant.FundConst;
import com.codi.bus.core.job.FundBasicJobService;
import com.codi.bus.core.service.OptionalService;
import com.codi.bus.core.service.T0Service;
import com.codi.bus.core.service.TradeSyncService;
import com.codi.bus.core.service.model.FundSortModel;
import com.codi.bus.core.service.model.RecommendedFundModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 模块名
 *
 * @author shi.pengyan
 * @version 1.0 2017-10-20 16:54
 * @since 1.0
 */
@Slf4j
@Component
public class FundBasicJobServiceImpl implements FundBasicJobService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private T0Service t0Service;

    @Autowired
    private OptionalService optionalService;

    @Autowired
    private TradeSyncService tradeSyncService;


    @Override
    public RecommendedFundModel refreshRecommendedFund() {
        log.info("refresh recommended fund");

        String namespace = CacheKeyConst.MF_FUND_NET_VALUE_PERFORMANCE_SORT;
        int fundTypeCode = FundConst.FUND_TYPE_CODE_CURRENCY;
        int statisType = 0;
        int sort = 1;//desc


        String key = CacheUtil.getKey(namespace, fundTypeCode, statisType, sort);

        ListOperations<String, String> listOp = redisTemplate.opsForList();

        int pageIndex = 0, pageSize = 10;

        boolean end = false;
        while (!end) {
            int from = pageIndex * pageSize;
            int to = (pageIndex + 1) * pageSize;

            List<String> temp = listOp.range(key, from, to);

            for (String aTemp : temp) {
                FundSortModel sortModel = JSON.parseObject(aTemp, FundSortModel.class);
                if (!sortModel.getFundName().toUpperCase().endsWith("B")) {
                    RecommendedFundModel model = new RecommendedFundModel();
                    model.setFundCode(sortModel.getFundCode());
                    model.setFundName(sortModel.getFundNameAbbr());
                    model.setLatestWeeklyYieldStr(sortModel.getRate());
                    model.setDailyProfitStr(sortModel.getValue());
                    model.setTradingDay(sortModel.getTradingDay());
                    // set cache
                    String redisKey = CacheUtil.getKey(CacheKeyConst.RECOMMEND_FUND_MODEL);
                    redisTemplate.opsForValue().set(redisKey, JSON.toJSONString(model));
                    log.info("bingo set recommend fund model suc.");
                    return model;
                }
            }
            pageIndex++;
        }

        log.warn("set recommend fund model error");
        return new RecommendedFundModel();
    }

    @Override
    public void refreshT0SortList() {
        log.info("refresh t0 sort order");

        t0Service.reloadCache();
    }

    @Override
    public void updateFundValue() throws BaseAppException {
        log.info("update optional fund value");
        optionalService.updateFundOptional();
    }

}
